package cn.xpride.framework.application;

import cn.xpride.framework.http.SmartHttpContext;
import cn.xpride.framework.http.annotation.*;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.smartboot.http.server.HttpRequest;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 这个是用来测试的类，测试是否能够获取到各种类型的参数
 */
@Slf4j
@RestController(value = "test",skip=true)
public class TestRestController {

    @GetMapping("get")
    public String get(@RestQuery("name") String name, @RestQuery("age") short age) throws Exception{
        log.info("收到get参数name={},age={}",name,age);
        HttpRequest request = SmartHttpContext.currentHttpRequest();
        return URLDecoder.decode(request.getQueryString(), StandardCharsets.UTF_8.name()) + " | " + System.currentTimeMillis();
    }

    @PostMapping("postWithQuery")
    public void postWithQuery(@RestQuery("name") String name, @RestQuery("age") Integer age, @RestBody JSONObject param) {
        log.info("收到postWithQuery参数name={},age={},param={}",name,age,param);
    }

    @PostMapping("postBean")
    public void postBean(@RestBody MapBody param) {
        log.info("收到postBean内容:{}", param.getKey().length());
    }

    @PostMapping("postBeanList")
    public void postBeanList(@RestBody List<MapBody> param) {
        log.info("收到postBean内容:{}", param.get(0).getKey());
    }

    @PostMapping("postBeanSet")
    public void postBeanSet(@RestBody Set<MapBody> param) {
        for (MapBody map : param) {
            System.out.println(map.getKey());
        }
        log.info("收到postSet内容:{}", param);
    }

    @PostMapping("postMap")
    public void postBean(@RestBody Map<String,Object> param) {
        log.info("收到postMap内容:{}", param.get("key"));
    }

    @PostMapping("postMapList")
    public void postMapList(@RestBody List<Map<String,Object>> param) {
        log.info("收到postMapSet内容:{}", param.get(0).get("key"));
    }

    @PostMapping("postMapSet")
    public void postMapSet(@RestBody Set<Map<String,Object>> param) {
        for (Map<String, Object> map : param) {
            System.out.println(map.get("key"));
        }
        log.info("收到postMapSet内容:{}", param);
    }

    @PostMapping("postString")
    public void postString(@RestBody String param) {
        log.info("收到postArray内容:{}", param);
    }

    @PostMapping("postStringList")
    public void postStringList(@RestBody List<String> param) {
        log.info("收到postArray内容:{}", param);
    }

    @PostMapping("postStringSet")
    public void postStringSet(@RestBody Set<String> param) {
        log.info("收到postArray内容:{}", param);
    }

    @PostMapping("postJSONObject")
    public void postJSONObject(@RestBody JSONObject param) {
        log.info("收到postJSONObject内容:{}", param);
    }

    @PostMapping("postJSONObjectList")
    public void postJSONObjectList(@RestBody List<JSONObject> param) {
        log.info("收到postJSONArray内容:{}", param.get(0).get("key"));
    }

    @PostMapping("postJSONObjectSet")
    public void postJSONObjectSet(@RestBody Set<JSONObject> param) {
        for (Map<String, Object> map : param) {
            System.out.println(map.get("key"));
        }
        log.info("收到postJSONArray内容:{}", param);
    }

}
